<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Marketing_card extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 */
	public function index($status = -1,$limit = 0)
	{
		//获取学习卡
		$this->load->model('School_learn_card_model');
		$card_list = $this->School_learn_card_model->get_learn_card($this->user_data['school_id'],$limit,$this->offset,$status);
		$this->smarty->assign("card_list", $card_list['result']);

		$this->load->library('pagination');
		$page_config['base_url'] = '/marketing_card/index/'.$status;
		$page_config['total_rows'] = $card_list['total_count'];
		$page_config['per_page'] = $this->offset ;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("menu", "marketing");
		$this->smarty->assign("status", $status);
		$this->smarty->display("marketing/marketing_card.html");
	}

	function add(){
		$post_data = $this->input->post();
		if($post_data){
			if(isset($post_data['course']) && count($post_data['course']) > 0){
				foreach ($post_data['course'] as $key => $value) {
					$code_info = get_field_by_where("o2o_school_system_video_course_codes", "count(id) AS count", "course_id='{$value}' AND status = 1 AND dongjie = 0");
					if(!$code_info || $code_info['count'] < $post_data['num']){
						show_message($this->jump_url, "视频课激活码数量不够。");exit;
					}
				}
			}

			if(isset($post_data['tiku']) && count($post_data['tiku']) > 0){
				foreach ($post_data['tiku'] as $key => $value) {
					$code_info = get_field_by_where("o2o_school_system_topic_codes", "count(id) AS count", "subject_id='{$value}' AND status = 1 AND dongjie = 0");
					if(!$code_info || $code_info['count'] < $post_data['num']){
						show_message($this->jump_url, "题库激活码数量不够。");exit;
					}
				}
			}

			if(isset($post_data['course']) && count($post_data['course']) > 0){
				foreach ($post_data['course'] as $key => $value) {
					$sql = "UPDATE {$this->db->dbprefix}o2o_school_system_video_course_codes SET dongjie = 1 WHERE status = 1 AND dongjie = 0 AND course_id='{$value}' LIMIT {$post_data['num']}";
					$this->db->query($sql);
				}
			}

			if(isset($post_data['tiku']) && count($post_data['tiku']) > 0){
				foreach ($post_data['tiku'] as $key => $value) {
					$sql = "UPDATE {$this->db->dbprefix}o2o_school_system_topic_codes SET dongjie = 1 WHERE status = 1 AND dongjie = 0 AND subject_id='{$value}' LIMIT {$post_data['num']}";
					$this->db->query($sql);
				}
			}
			
			$this->load->model('School_learn_card_model');
			$result = $this->School_learn_card_model->generate_learn_card($post_data,$this->user_data['school_id']);
			header("Location:/marketing_card");exit;
		}

		//获取视频课
		$this->load->model('Courseo2o_model');
		$course = $this->Courseo2o_model->get_course_all_by_school($this->user_data['school_id'],1,1);
		$this->smarty->assign("course", $course);

		//获取题库
		$this->load->model("Topics");
		$subject = $this->Topics->get_subject($this->session->userdata['user_data']['school_id']);
		$this->smarty->assign("tiku", $subject);

		//获取实训
		$data = json_decode(file_get_contents("http://123.206.137.187:804/HandlerData.ashx?action=GetZhangtaoInfo"),true);
		$shixun_data = $shixun_list = array();
		if(!empty($data['rows']) && count($data['rows']) > 0){
			$this->load->model("Shixuns");
			$ids = '';
			foreach ($data['rows'] as $key => $value) {
				$shixun_list[$value['Id']] = $value;
				if($key == 0){
					$ids .= "'" . $value['Id'] . "'";
				}else{
					$ids .= ",'" . $value['Id'] . "'";
				}
			}
			$pack_list = $this->Shixuns->get_sx_packages($ids);
			if($pack_list){
				foreach ($pack_list as $k => $v) {
					if(isset($shixun_list[$v['sx_id']]) && $v['status'] == 1){
						$shixun_list[$v['sx_id']]['status'] = $v['status'];
						$shixun_list[$v['sx_id']]['pack_id'] = $v['id'];
						$shixun_data[] = $shixun_list[$v['sx_id']];
					}
				}
			}
		}
		$this->smarty->assign("shixun", $shixun_data);
		$this->smarty->assign("menu", "marketing");
		$this->smarty->display("marketing/marketing_card_add.html");
	}
}
